package com.jsyxdl.k3cloud.listener.handling;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.hustcad.plm.pdm.flowbusiness.model.entity.FlowObject;
import com.hustcad.plm.pdm.partbom.dto.ChildPartsFiltersDTO;
import com.hustcad.plm.pdm.partbom.dto.PartBomTree;
import com.hustcad.plm.pdm.partbom.dto.QuerySubstituteLinkDTO;
import com.hustcad.plm.pdm.partbom.service.TyplmPartBomService;
import com.hustcad.plm.pdm.partbom.service.partalternate.TyplmPdmPartAlternateService;
import com.hustcad.plm.pdm.partbom.service.partalternate.TyplmPdmPartSubstituteService;
import com.hustcad.plm.pdm.partbom.vo.AlternateInfoVO;
import com.hustcad.plm.pdm.partbom.vo.PartBomTreeByFilterVO;
import com.hustcad.plm.pdm.partbom.vo.PartDetailsVO;
import com.hustcad.plm.pdm.partbom.vo.PartSubstituteVO;
import com.jsyxdl.k3cloud.listener.K3CloudHttpService;
import com.jsyxdl.k3cloud.service.ErpWorkedTaskService;
import com.jsyxdl.k3cloud.utils.PlmPartOptionUtils;
import com.jsyxdl.service.ErpPartAlternativeService;
import com.ty.basic.entity.org.UserDO;
import com.ty.basic.session.TyAccountContext;
import org.apache.commons.lang3.ObjectUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 替代方案物料处理服务
 */
public class AlternativeMaterialsHandling {

    public JSONObject productionCall(String check,Map<String, Object> executionType, FlowObject flowObject) throws Exception {

        PlmPartOptionUtils plmPartOptionUtils = SpringUtil.getBean(PlmPartOptionUtils.class);
        TyplmPdmPartAlternateService typlmPartAlternateService = SpringUtil.getBean(TyplmPdmPartAlternateService.class);
        TyplmPdmPartSubstituteService typlmPartSubstituteService = SpringUtil.getBean(
                TyplmPdmPartSubstituteService.class);
        ErpPartAlternativeService erpPartAlternativeService = SpringUtil.getBean(ErpPartAlternativeService.class);

        String k3cloud = (String) executionType.get("k3cloud");

        BigInteger oid = flowObject.getRefObjectOid();
        String oType = flowObject.getRefObjectOtype();
        PartDetailsVO partDetailsVO = plmPartOptionUtils.getPartDetailsVO(oid, oType);

        TyplmPartBomService typlmPartBomService = SpringUtil.getBean(TyplmPartBomService.class);
        if (typlmPartBomService != null) {
            com.hustcad.plm.pdm.partbom.dto.ExpandBomTreeByFilterDTO expandBomTreeByFilterDTO = new com.hustcad.plm.pdm.partbom.dto.ExpandBomTreeByFilterDTO();
            expandBomTreeByFilterDTO.setAttrFilterCondition(new ArrayList<>());

            ChildPartsFiltersDTO childPartsFiltersDTO = new ChildPartsFiltersDTO();
            childPartsFiltersDTO.setBaseLineOid("");
            childPartsFiltersDTO.setDate("");
            childPartsFiltersDTO.setExpandCondition("latest");
            childPartsFiltersDTO.setLifecycleStateKey("");
            childPartsFiltersDTO.setPurposeOfBom("");
            childPartsFiltersDTO.setSpareBom("");
            childPartsFiltersDTO.setView(partDetailsVO.getView());
            List<ChildPartsFiltersDTO> list = new ArrayList<>();
            list.add(childPartsFiltersDTO);
            expandBomTreeByFilterDTO.setConditions(list);

            expandBomTreeByFilterDTO.setExpandLevel("N");
            expandBomTreeByFilterDTO.setFatherNumber(partDetailsVO.getObjectNumber());
            expandBomTreeByFilterDTO.setFatherOid(String.valueOf(partDetailsVO.getOid()));
            expandBomTreeByFilterDTO.setFatherOtype(partDetailsVO.getOtype());
            expandBomTreeByFilterDTO.setFilterMode("0");
            expandBomTreeByFilterDTO.setGiveUsesOccurrence("N");
            expandBomTreeByFilterDTO.setIbaColumns(new ArrayList<>());
            expandBomTreeByFilterDTO.setIsApplyRoot(false);
            expandBomTreeByFilterDTO.setIsFilterAttr(false);
            expandBomTreeByFilterDTO.setIsNeedTranEnum(true);
            expandBomTreeByFilterDTO.setIsNestedTree(false);
            expandBomTreeByFilterDTO.setIsQueryIBA(true);

            PartBomTreeByFilterVO result = typlmPartBomService.expandBomTreeByFilterNew(expandBomTreeByFilterDTO,
                                                                                        Boolean.TRUE);
            UserDO userDO = TyAccountContext.getUser();

            Map<String, Object> cmdAlternative = new HashMap<>();
            Map<String, Object> cmdSubstitute = new HashMap<>();

            List<Map<String, Object>> alternativeList = new ArrayList<>();
            List<PartDetailsVO> partDetailsVOList = new ArrayList<>();
            for (int y = 0; y < result.getPartBomTreeList().size(); y++) {
                PartBomTree partBomTree = result.getPartBomTreeList().get(y);
                List<AlternateInfoVO> listAlternates = null;
                List<PartSubstituteVO> listSubstitute = null;
                if (cmdAlternative.containsKey(partBomTree.getObjectNumber()) == false) {
                    List<String> alternateForPartNumbers = new ArrayList<>();
                    alternateForPartNumbers.add(partBomTree.getObjectNumber());
                    //查询全局替代件
                    listAlternates = typlmPartAlternateService.batchQueryAlternates(alternateForPartNumbers);
                    if (listAlternates != null && listAlternates.size() > 0) {
                        cmdAlternative.put(partBomTree.getObjectNumber(), "0");
                    }
                }
                if (cmdSubstitute.containsKey(partBomTree.getObjectNumber()) == false) {
                    //查询局部替代件
                    QuerySubstituteLinkDTO querySubstituteLinkDTO = new QuerySubstituteLinkDTO();
                    if (ObjectUtils.isNotEmpty(userDO) && ObjectUtils.isNotEmpty(userDO.getOid())) {
                        querySubstituteLinkDTO.setUserOid(userDO.getOid());
                    }
                    querySubstituteLinkDTO.setAoid(String.valueOf(partBomTree.getAoid()));
                    querySubstituteLinkDTO.setBnumber(String.valueOf(partBomTree.getObjectNumber()));
                    querySubstituteLinkDTO.setBoid(String.valueOf(partBomTree.getPartoid()));
                    listSubstitute = typlmPartSubstituteService.queryPartSubstituteLink(querySubstituteLinkDTO);
                    if (listSubstitute != null && listSubstitute.size() > 0) {
                        cmdSubstitute.put(partBomTree.getObjectNumber(), "1");
                    }
                }

                Map<String, Object> alternative = new HashMap<>();
                if ("全局替代方案".equals(k3cloud)) {
                    if (listAlternates != null && listAlternates.size() > 0) {
                        alternative.put("listAlternates", listAlternates);
                    }
                }
                if ("局部替代方案".equals(k3cloud)) {
                    if (listSubstitute != null && listSubstitute.size() > 0) {
                        alternative.put("listSubstitute", listSubstitute);
                    }
                }


                if (alternative.size() > 0) {
                    BigInteger cpartOid = new BigInteger(partBomTree.getPartoid());
                    String cpartType = partBomTree.getPartotype();
                    PartDetailsVO partDetails = plmPartOptionUtils.getPartDetailsVO(cpartOid, cpartType);
                    alternative.put("partDetails", partDetails);
                    alternativeList.add(alternative);
                }
            }


            //首先创建替代件物料
            for (int i = 0; i < alternativeList.size(); i++) {
                Map<String, Object> alternative = alternativeList.get(i);
                List<AlternateInfoVO> listAlternates = (List<AlternateInfoVO>) alternative.get("listAlternates");
                List<PartSubstituteVO> listSubstitute = (List<PartSubstituteVO>) alternative.get("listSubstitute");
                if (listAlternates != null) {
                    for (int a = 0; a < listAlternates.size(); a++) {
                        AlternateInfoVO alternateInfoVO = listAlternates.get(a);
                        List<Map<String, Object>> erpList = erpPartAlternativeService.getPartDetailsBylinkOid(
                                new BigInteger(alternateInfoVO.getId()));
                        if (erpList.size() > 0) {
                            oid = new BigInteger(String.valueOf(erpList.get(0).get("linkpartoid")));
                            oType = (String) erpList.get(0).get("linkotype");
                            PartDetailsVO linkPartDetailsVO = plmPartOptionUtils.getPartDetailsVO(oid, oType);
                            partDetailsVOList.add(linkPartDetailsVO);
                        }
                    }
                }
                if (listSubstitute != null) {
                    for (int a = 0; a < listSubstitute.size(); a++) {
                        PartSubstituteVO partSubstituteVO = listSubstitute.get(a);
                        List<Map<String, Object>> erpList = erpPartAlternativeService.getPartDetailsBySubstituteLinkOid(
                                new BigInteger(partSubstituteVO.getLinkId()));
                        if (erpList.size() > 0) {
                            oid = new BigInteger(String.valueOf(erpList.get(0).get("linkpartoid")));
                            oType = (String) erpList.get(0).get("linkotype");
                            PartDetailsVO linkPartDetailsVO = plmPartOptionUtils.getPartDetailsVO(oid, oType);
                            partDetailsVOList.add(linkPartDetailsVO);
                        }
                    }
                }
            }

            MaterialsBatchHandling materialsBatchHandling = new MaterialsBatchHandling();
            JSONObject erpResult = materialsBatchHandling.productionCall(check,executionType, partDetailsVOList);
            return erpResult;
        }


        return null;
    }

}
